<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模板管理</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f7f7f7;
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px;
            background-color: #fff;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            border-bottom: 2px solid #ddd;
            padding-bottom: 15px;
        }

        .header h1 {
            margin: 0;
            font-size: 2.5em;
            color: #007bff;
            font-weight: bold;
        }

        .header .actions button {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1em;
            transition: background-color 0.3s;
        }

        .header .actions button:hover {
            background-color: #0056b3;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 1.1em;
            color: #555;
        }

        .form-group input, .form-group button {
            width: 100%;
            padding: 12px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1em;
        }

        .form-group input:focus, .form-group button:focus {
            border-color: #007bff;
            outline: none;
        }

        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1em;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        .image-list {
            list-style: none;
            padding: 0;
            margin-bottom: 30px;
        }

        .image-list li {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 15px;
            border-bottom: 1px solid #eee;
            transition: background-color 0.3s;
        }

        .image-list li:hover {
            background-color: #f9f9f9;
        }

        .image-list img {
            width: 120px;
            height: 120px;
            object-fit: cover;
            border-radius: 5px;
            margin-right: 15px;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 30px;
        }

        .pagination button {
            padding: 8px 15px;
            margin: 0 5px;
            cursor: pointer;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #fff;
            color: #333;
            font-size: 1em;
            transition: background-color 0.3s, color 0.3s, border-color 0.3s;
        }

        .pagination button:hover {
            background-color: #007bff;
            color: #fff;
            border-color: #007bff;
        }

        .pagination button.disabled {
            cursor: not-allowed;
            opacity: 0.5;
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.5);
            backdrop-filter: blur(5px);
            padding-top: 60px;
        }

        .modal-content {
            background-color: #fff;
            margin: 5% auto;
            padding: 30px;
            border: 1px solid #888;
            border-radius: 8px;
            width: 80%;
            max-width: 600px;
        }

        .close {
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }

        .alert {
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid transparent;
            border-radius: 4px;
        }

        .alert-success {
            color: #3c763d;
            background-color: #dff0d8;
            border-color: #d6e9c6;
        }

        .alert-danger {
            color: #a94442;
            background-color: #f2dede;
            border-color: #ebccd1;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>模板管理</h1>
        <div class="actions">
            <button onclick="openUploadModal('single')">上传模板</button>
        </div>
    </div>

    <!-- 图片列表 -->
    <ul id="image-list-items" class="image-list"></ul>

    <!-- 分页导航 -->
    <div class="pagination">
        <button onclick="fetchTemplates(1)" id="first-page" class="disabled">首页</button>
        <button onclick="fetchTemplates(currentPage - 1)" id="prev-page" class="disabled">上一页</button>
        <span id="page-info"></span>
        <button onclick="fetchTemplates(currentPage + 1)" id="next-page" class="disabled">下一页</button>
        <button onclick="fetchTemplates(totalPages)" id="last-page" class="disabled">末页</button>
    </div>
</div>

<!-- 上传模板模态框 -->
<div id="upload-single-modal" class="modal">
    <div class="modal-content">
        <span class="close" onclick="closeUploadModal('single')">&times;</span>
        <h2>上传模板</h2>
        <div class="form-group">
            <label for="gifFile">选择 GIF 文件</label>
            <input type="file" id="gifFile" accept="image/gif">
        </div>
        <div class="form-group">
            <label for="templateFile">选择模板文件</label>
            <input type="file" id="templateFile" accept=".txt">
        </div>
        <button onclick="uploadTemplate('single')">上传</button>
        <div id="message-single-modal" class="alert"></div>
    </div>
</div>

<script>
    const apiBaseUrl = '/api/templates';
    let currentPage = 1;
    let totalPages = 1;

    // 打开上传模态框
    function openUploadModal(type) {
        document.getElementById(`upload-${type}-modal`).style.display = 'block';
    }

    // 关闭上传模态框
    function closeUploadModal(type) {
        document.getElementById(`upload-${type}-modal`).style.display = 'none';
        document.getElementById(`message-${type}-modal`).className = 'alert';
        document.getElementById(`message-${type}-modal`).textContent = '';
    }

    // 上传模板
    function uploadTemplate(type) {
        const gifFileInput = document.getElementById('gifFile');
        const templateFileInput = document.getElementById('templateFile');
        const gifFile = gifFileInput.files[0];
        const templateFile = templateFileInput.files[0];
        const messageDiv = document.getElementById(`message-${type}-modal`);

        if (!gifFile || !templateFile) {
            messageDiv.className = 'alert alert-danger';
            messageDiv.textContent = '请选择一个 GIF 文件和一个模板文件';
            return;
        }

        const formData= new FormData();
formData.append('gifFile', gifFile);
formData.append('templateFile', templateFile);    fetch(`${apiBaseUrl}/add`, {
        method: 'POST',
        body: formData
    })
    .then(response => response.json())
    .then(data => {
        messageDiv.className = 'alert alert-success';
        messageDiv.textContent = '模板上传成功，ID: ' + data.templateId;
        closeUploadModal(type);
        fetchTemplates();
    })
    .catch(error => {
        messageDiv.className = 'alert alert-danger';
        messageDiv.textContent = error.message;
    });
}

// 获取模板列表
function fetchTemplates(page = 1) {
    const imageListItems = document.getElementById('image-list-items');
    const pageInfo = document.getElementById('page-info');
    const firstPageBtn = document.getElementById('first-page');
    const prevPageBtn = document.getElementById('prev-page');
    const nextPageBtn = document.getElementById('next-page');
    const lastPageBtn = document.getElementById('last-page');

    fetch(`${apiBaseUrl}/list`, {
        method: 'POST'
    })
    .then(response => response.json())
    .then(data => {
        imageListItems.innerHTML = '';
        data.forEach(template => {
            const li = document.createElement('li');
            const img = document.createElement('img');
            img.src = `data:image/gif;base64,${template.gifBase64}`;
            img.alt = template.templateId;
            img.style.width = '120px';
            img.style.height = '120px';
            img.style.objectFit = 'cover';
            img.style.borderRadius = '5px';
            img.style.marginRight = '15px';
            const deleteButton = document.createElement('button');
            deleteButton.textContent = '删除';
            deleteButton.onclick = () => deleteTemplate(template.templateId);
            li.appendChild(img);
            li.appendChild(document.createTextNode(template.templateId));
            li.appendChild(deleteButton);
            imageListItems.appendChild(li);
        });

        currentPage = page;
        totalPages = Math.ceil(data.length / 10);

        pageInfo.textContent = `第 ${currentPage} 页 / 共 ${totalPages} 页`;

        firstPageBtn.disabled = currentPage === 1;
        prevPageBtn.disabled = currentPage === 1;
        nextPageBtn.disabled = currentPage === totalPages;
        lastPageBtn.disabled = currentPage === totalPages;
    })
    .catch(error => {
        alert(error.message);
    });
}

// 删除模板
function deleteTemplate(templateId) {
    fetch(`${apiBaseUrl}/del/${templateId}`, {
        method: 'POST'
    })
    .then(response => response.text())
    .then(data => {
        alert(data);
        fetchTemplates();
    })
    .catch(error => {
        alert(error.message);
    });
}

// 初始化
fetchTemplates();</script> </body> </html>